
const tools = require('../util/tool')
module.exports = class user_sql extends require('./mysql'){

    /**
     * 登录
     * @param username
     * @param password
     * @param type
     * @returns {Promise<any>}
     * @constructor
     */
    static LoginUser(username,password,type){
        type = Number(type)
        return new Promise((resolve, reject) =>{
            let sql = "select * from user where username='"+username+"' and password='"+password+"'"
            console.log(sql)
            this.query(sql).then(result=>{
                resolve(result)
            }).catch(err=>{
                reject('登录失败')
            })
        } )
    }

    /**
     * 根据用户类型进行用户信息获取
     * @param type
     * @param currPage
     * @param pageNum
     */
    static getUsersByTypePageMod(type,currPage,pageNum){
        pageNum = Number(pageNum);
        currPage = Number(currPage);
        currPage = Number(currPage * pageNum);
        return new Promise((resolve, reject) => {
            let sql = 'select * from user where type = '+ type+' LIMIT ?,?'
            console.log(sql)
            this.query(sql,this.formarParams(currPage,pageNum)).then(result=>{
                resolve(result)
            }).catch(err=>{
                reject(err)
            })
        })
    }
    static getUsersByTypePageTotal(type){
        return new Promise((resolve, reject) => {
            let sql = 'select count(1) as count from user where type = '+type
            console.log(sql)
            this.query(sql).then(result=>{
                resolve(result)
            }).catch(err=>{
                reject(err)
            })
        })
    }

    /**
     * 获取用户总表信息
     * @param currPage
     * @param pageNum
     */
    static getUsersDataMod(currPage,pageNum){
        pageNum = Number(pageNum);
        currPage = Number(currPage);
        currPage = Number(currPage * pageNum)
        return new Promise((resolve , reject)=>{
            let sql = "select * from `user` order by createtime desc limit ?,?"
            console.log(sql)
            this.query(sql,this.formarParams(currPage,pageNum)).then(result=>{
                resolve(result)
            }).catch(err=>{
                reject(err)
            })
        })
    }
    static getUsersDataTotal(){
        return new Promise((resolve, reject) => {
            let sql = "select count(1) as count from `user`"
            console.log(sql)
            this.query(sql).then(result=>{
                resolve(result)
            }).catch(err=>{
                reject(err)
            })
        })
    }


    /**
     * 删除用户表用户
     * @param id
     */
    static delUserDataMod(id){
        return new Promise((resolve, reject) => {
            let sql = 'delete from user where id ='+id;
            console.log(sql);
            this.query(sql).then(result=>{
                resolve('删除用户成功')
            }).catch(err=>{
                reject('删除用户失败')
            })
        })
    }

    /**
     * 用户编辑
     * @param req
     * @param resp
     * 
     */
    static upUserDataMod(u_id,username,password,nickname,sex,homeaddress,type,email,note){
        let currTime = tools.formatDate()
        return new Promise((resolve, reject) => {
            let sql = 'update `user` set username="'+username+'", password="'+password+'", nickname="'+nickname+'", sex="'+sex+'", homeaddress="'+homeaddress+'", type="'+type+'", email="'+email+'", note="'+note+'", createtime="'+currTime+'" where id ='+u_id
            console.log(sql)
            this.query(sql).then(result=>{
                resolve('更新成功')
            }).catch(err=>{
                reject('更新失败')
            })
        })
    }


    /**
     * 用户新增
     * @param req
     * @param resp
     *
     */
    static addUserDataMod(id,username,password,type,email,nickname,sex,homeaddress,note,createtime){

        return new Promise((resolve, reject) => {
            let sql = "insert into user(id,username,password,type,email,nickname,sex,homeaddress,note,createtime) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
            let createtime = tools.formatDate()
            // let value = [u_id,username,password,type,email,nickname,sex,homeaddress,note,createtime];
            console.log(sql)
            this.query(sql,this.formarParams(id,username,password,type,email,nickname,sex,homeaddress,note,createtime)).then(result=>{
                resolve('添加成功')
            }).catch(err=>{
                reject('添加失败')
            })
        })
    }


    /**
     * 密码修改
     * @param u_id
     * @param oldPwd
     * @param newPwd
     * @returns {Promise<any>}
     */
    static updatePwdSql(newPwd,id,oldPwd){
        return new Promise((resolve, reject)=>{
            // let sql='update `user` set password = ? where password = ? and id = ?'
            let sql = "UPDATE `user` SET password=\ '" + newPwd + "'\ WHERE password =\ '" + oldPwd+"'\ and id="+id;
            this.query(sql).then(result=>{
                resolve(result)
            }).catch(err=>{
                reject(err)
            })
        })
    }


    /**
     * 根据id获取我的信息
     * @param id
     * @returns {Promise<any>}
     */
    static getMyIdSql(id){
        return new Promise((resolve , reject)=>{
            let sql = "select * from `user` where id = "+ id;
            console.log(sql);
            this.query(sql).then(result=>{
                resolve(result)
            }).catch(err=>{
                reject(err)
            })
        })
    }


    /**
     * 修改头像
     * @param headImg
     * @param u_id
     * @returns {Promise<any>}
     */
    static upHeadSql(head_imgUrl,u_id){
        return new Promise((resolve, reject)=>{
            let sql='update `user` set `icon` = "'+head_imgUrl+'" where id = '+u_id;
            console.log(sql)
            this.query(sql).then(result=>{
                resolve("上传成功")
            }).catch(err=>{
                reject(err)
            })
        })
    }


    /**
     * 修改用户车名和状态
     * @param u_id
     * @param parkname
     * @param parkstatus
     * @param parkId
     * @returns {Promise<any>}
     */
    static updateParkStatusSql(u_id,parkname,parkstatus,parkId){
        return new Promise((resolve, reject) => {
            let sql = 'update `user` set parkname="'+parkname+'",parkstatus="'+parkstatus+'",parkId="'+parkId+'" where id ='+u_id
            console.log(sql);
            this.query(sql).then(result=>{
                resolve('更新成功')
            }).catch(err=>{
                reject('更新失败')
            })
        })
    }


    /**
     * 修改用户车位状态
     * @param parkId
     * @param parkstatus
     * @param parkname
     * @returns {Promise<any>}
     */
    static upDateParkSql(parkId,parkstatus,parkname){
        return new Promise((resolve, reject) => {
            let sql = 'update `user` set parkstatus="'+parkstatus+'",parkname="'+parkname+'" where parkId ='+parkId
            console.log(sql);
            this.query(sql).then(result=>{
                resolve('更新成功')
            }).catch(err=>{
                reject('更新失败')
            })
        })
    }

    static upParkSql(u_id,parkstatus){
        return new Promise((resolve, reject) => {
            let sql = 'update `park` set parkstatus="'+parkstatus+'" where id ='+u_id
            console.log(sql);
            this.query(sql).then(result=>{
                resolve('更新成功')
            }).catch(err=>{
                reject('更新失败')
            })
        })
    }


    /**
     * 登录日志录入
     * @param username
     * @param type
     * @param email
     * @param createtime
     * @returns {Promise<any>}
     */
    static addloginDataSql(username,type,email,createtime){

        return new Promise((resolve, reject) => {
            let sql = "insert into `loginLog`(username,type,email,createtime) values (?, ?, ?, ?)";
            let createtime = tools.formatDate()
            // let value = [u_id,username,password,type,email,nickname,sex,homeaddress,note,createtime];
            console.log(sql)
            this.query(sql,this.formarParams(username,type,email,createtime)).then(result=>{
                resolve('添加成功')
            }).catch(err=>{
                reject('添加失败')
            })
        })
    }


    /**
     * 登录日志列表获取
     * @param username
     * @param type
     * @param startDate
     * @param endDate
     * @param currPage
     * @param pageNum
     * @returns {Promise<any>}
     */
    static getLoginDataSql(username,type,startDate,endDate,currPage,pageNum){
        pageNum = Number(pageNum);
        currPage = Number(currPage);
        currPage = Number(pageNum * currPage);
        return new Promise((resolve, reject)=>{
            // let sql="select * from `user` where username like '%"+username+"%' and type="+type+" limit ?,?"
            let sql="select * from `loginLog` where 1=1";
            if (username) sql += " and username like '%"+username+"%'";
            if (type) sql += " and type= '" + type + "'";
            if (startDate && endDate) sql += " and (createtime BETWEEN '"+startDate+"' AND '"+endDate+"')";
            sql += " order by createtime desc limit ?,?";
            console.log(sql);
            this.query(sql,this.formarParams(currPage,pageNum)).then(result=>{
                resolve(result)
            }).catch(err=>{
                reject(err)
            })
        })
    }

    /**
     * 登录日志列表获取
     * @param username
     * @param type
     * @param startDate
     * @param endDate
     * @returns {Promise<any>}
     */
    static getLoginDataTotal(username,type,startDate,endDate){
        return new Promise((resolve, reject)=>{
            // let sql="select count(1) as count from `user` where username like '%"+username+"%' and type="+type+" "
            let sql='select count(1) as count from `loginLog` where 1=1';
            if (username) sql += " and username like '%"+username+"%'";
            if (type) sql += " and type= '" + type + "'";
            if (startDate && endDate) sql += " and (createtime BETWEEN '"+startDate+"' AND '"+endDate+"')";
            console.log(sql);
            this.query(sql).then(result=>{
                resolve(result)
            }).catch(err=>{
                reject(err)
            })
        })
    }

    /**
     * 删除日志
     * @param id
     * @returns {Promise<any>}
     */
    static delLoginDataSql(id){
        return new Promise((resolve, reject) => {
            let sql = 'delete from `loginLog` where id ='+id;
            console.log(sql);
            this.query(sql).then(result=>{
                resolve('删除日志成功')
            }).catch(err=>{
                reject('删除日志失败')
            })
        })
    }


    /**
     * 获取当月用户登录情况
     * @param nowDate
     * @param lastDate
     * @returns {Promise<any>}
     */
    static getLoginMonthSql(nowDate, lastDate) {
        return new Promise((resolve, reject) => {
            let sql = "select * from `loginLog` where createtime between ? and ?"
            this.query(sql, this.formarParams(nowDate, lastDate)).then(result => {
                resolve(result)
            }).catch(err => {
                reject(err)
            })
        })
    }

    /**
     * 获取用户新增量
     * @param nowDate
     * @param lastDate
     * @returns {Promise<any>}
     */
    static getLogMonthSql(nowDate, lastDate) {
        return new Promise((resolve, reject) => {
            let sql = "select * from `user` where createtime between ? and ?"
            this.query(sql, this.formarParams(nowDate, lastDate)).then(result => {
                resolve(result)
            }).catch(err => {
                reject(err)
            })
        })
    }


    /**
     * 获取模糊用户表
     * @param username
     * @param currPage
     * @param pageNum
     * @returns {Promise<any>}
     */
    static getUserListSql(username,currPage,pageNum){
        pageNum = Number(pageNum);
        currPage = Number(currPage);
        currPage = Number(pageNum * currPage);
        return new Promise((resolve, reject)=>{
            // let sql="select * from `user` where username like '%"+username+"%' and type="+type+" limit ?,?"
            let sql="select * from `user` where 1=1";
            if (username) sql += " and username like '%"+username+"%'";
            sql += " limit ?,?";
            console.log(sql);
            this.query(sql,this.formarParams(currPage,pageNum)).then(result=>{
                resolve(result)
            }).catch(err=>{
                reject(err)
            })
        })
    }


    /**
     * 模糊用户表总数
     * @param username
     * @returns {Promise<any>}
     */
    static getUserListTotal(username){
        return new Promise((resolve, reject)=>{
            // let sql="select count(1) as count from `user` where username like '%"+username+"%' and type="+type+" "
            let sql='select count(1) as count from `user` where 1=1';
            if (username) sql += " and username like '%"+username+"%'";
            console.log(sql);
            this.query(sql).then(result=>{
                resolve(result)
            }).catch(err=>{
                reject(err)
            })
        })
    }


}
    
